﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using GameLayer;
using Microsoft.Xna.Framework;
using MainGame.GameForm;
using Microsoft.Xna.Framework.Content;
using System.Xml;

namespace GameBase
{
    public class FormGroup : VisibleGameEntity<FormControl>
    {
        private Sprite _background;
        private MainGame.MainGame _game;

        public MainGame.MainGame Game
        {
            get 
            {
                return _game; 
            }
            set {
                _game = value;
                foreach (var i in this.Items) 
                {
                    i.Game = value;
                }
            }
        }
        public Sprite Background
        {
            get { return _background; }
            set { _background = value; }
        }

        public override bool ListenEvent
        {
            get
            {
                return base.ListenEvent;
            }
            set
            {
                base.ListenEvent = value;
                foreach (var button in this.Items)
                {
                    button.ListenEvent = value;
                }
            }
        }

        public override Microsoft.Xna.Framework.Vector2 Position
        {
            get
            {
                return base.Position;
            }
            set
            {
                Vector2 d = value - Position;
                foreach (var item in this.Items)
                {
                    item.Position += d;
                }
                base.Position = value;
            }
        }

        public override Vector2 Size
        {
            get
            {
                return _size;
            }
            set
            {
                _size = value;
            }
        }

        protected override void UpdateEntitys(Microsoft.Xna.Framework.GameTime gameTime, object extraData)
        {
            base.UpdateEntitys(gameTime, extraData);
            if (Background != null)
            {
                Background.Update(gameTime);
                Background.NextFrame();

            }
            foreach (var item in this.Items)
            {
                item.Update(gameTime, extraData);
            }
        }

        protected override void Paint(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, object extraData)
        {
            if (Background != null)
            {
                Vector2 pos = this.Position;
                Vector2 size = this.Size;
                Rectangle bound = new Rectangle((int)pos.X, (int)pos.Y, (int)size.X, (int)size.Y);
                Background.Draw(gameTime, spriteBatch, bound, Color.White);
            }
            foreach (var item in this.Items)
            {
                item.Draw(gameTime, spriteBatch, extraData);
            }
        }

        public static FormGroup LoadFormGroup(ContentManager content, String xmlResource)
        {
            FormGroup frmGroup = new FormGroup();
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlResource);
            XmlElement root = doc.DocumentElement;
            XmlElement xmlBackground = root.SelectSingleNode("Sprite[@name='background']") as XmlElement;
            if (xmlBackground != null)
            {
                frmGroup.Background = new Sprite(content, xmlBackground);
            }
            XmlNodeList xmlForms = root.SelectNodes("FormControls/FormControl");
            foreach (XmlElement xmlForm in xmlForms)
            {
                FormControl frmControl = FormControl.LoadFormControl(content, xmlForm);
                frmGroup.Add(frmControl);
            }
            int x, y;
            if (!int.TryParse(root.GetAttribute("x"), out x))
            {
                x = 0;
            }
            if (!int.TryParse(root.GetAttribute("y"), out y))
            {
                y = 0;
            }

            frmGroup.Position = new Vector2(x, y);
            int width, height;
            if (!int.TryParse(root.GetAttribute("width"), out width))
            {
                width = 0;
            }
            if (!int.TryParse(root.GetAttribute("height"), out height))
            {
                height = 0;
            }
            frmGroup.Size = new Vector2(width, height);
            return frmGroup;
        }
    }
}
